// pages/myCards/myCards.js
const app = getApp()
const ApiRootUrl = 'http://8.136.195.245:8000/';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    MyCardList: [],
    cardNum: 0,
    cardUid: "",

    androidDialog2_unbinduser: false,
    toast: false,
    warnToast: false,
    hideToast: false,
    hideWarnToast: false,
    warn401Toast: false,
    hide401Toast: false,
    scanStr: "",
    resmsg: "",
    cardUid: "",
    MyCardPictureList: [{
        fruitPicture: '/image/card1.png'
      },
      {
        fruitPicture: '/image/card2.png'
      },
      {
        fruitPicture: '/image/card3.jpg'
      },
      {
        fruitPicture: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2453398919,3408790812&fm=26&gp=0.jpg'
      },
      {
        fruitPicture: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1070649221,78984387&fm=26&gp=0.jpg'
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyCards()
  },
  // 卡片账单
  navigatetoCardRecord() {
    wx.switchTab({
      url: '/pages/order/order'
    })
  },

    // 卡片充值
    navigatetoCardRecord() {
      wx.switchTab({
        url: '/pages/home/home'
      })
    },
  getMyCards: function () {
    var that1 = this
    wx.request({
      // url: "http://localhost:8001/card/list/" + app.globalData.userId,
      url:ApiRootUrl+"card/list/" + app.globalData.userId,
      // url: "http://8.136.195.245:8000/wechat/wxLogin",
      data: {},
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        console.log("res=", res)
        console.log("获取卡片函数res.data.data=", res.data.data)
        that1.setData({
          MyCardList: res.data.data
        })
        if (that1.data.MyCardList == null) {
          that1.setData({
            ['MyCardList[' + 0 + '].url']: "/image/card0.png",
          })
        } else {
          that1.setData({
            cardNum: that1.data.MyCardList.length,
          })
          for (let i = 0; i < that1.data.MyCardList.length; i++) {
            var that2 = that1
            that1.setData({
              // ['sortList[' + 0 + '].hotrange']: 'hot1',
              ['MyCardList[' + i + '].cardPicture']: that2.data.MyCardPictureList[i].fruitPicture,
            })
          }
        }

        console.log("赋值后MyCardList=", that1.data.MyCardListt)
      },
      fail: function (err) {}, //请求失败
      complete: function () {} //请求完成后执行的函数
    })
  },

  //解绑卡片按钮的按下后显示该弹窗
  unbindUserToast: function (e) {
    console.log("carduid=",e.currentTarget.dataset.carduid)
    this.setData({
      cardUid: e.currentTarget.dataset.carduid,
      androidDialog2_unbinduser: true
    })
  },
  //调用api生成解绑用户
  unbindUserApi: function () {
    let that = this
    console.log("你在进行api请求")
    console.log("userid:", app.globalData.userId)
    wx.request({
      // url: 'http://localhost:8001/card/unbindUser',
      url:ApiRootUrl+'card/unbindUser',
      // url: ApiRootUrl + 'card/unbindUser',
      data: {
        userId: app.globalData.userId,
        cardUid: that.data.cardUid,
      },
      method: 'PUT',
      header: {
        'content-type': 'application/json',
        'chartset': 'utf-8'
      },
      success(res) {
        console.log("获取数据成功", res)
        console.log("code:", res.data.code)
        that.setData({
          msg: res.data.message
        })
        console.log("res.data.message", res.data.message)
        if (res.data.code == 200) {
          that.openToast()
        }
        if (res.data.code == 400) {
          that.openWarnToast()
        }
        if (res.data.code == 401) {
          that.open401Toast()
        }
      }
    })
  },
  closeUnbind: function () {
    this.setData({
      androidDialog2_unbinduser: false,
    })

  },
  confirmUnbind: function () {
    console.log("你点击了确定充值按钮")
    this.setData({
      androidDialog2_unbinduser: false,
    })
    this.unbindUserApi()
  },


    // //解绑充值按钮的按下后显示该弹窗
    // ChargeonlineToast: function (e) {
    //   console.log("carduid=",e.currentTarget.dataset.carduid)
    //   this.setData({
    //     cardUid: e.currentTarget.dataset.carduid,
    //     androidDialog2_unbinduser: true
    //   })
    // },
    // //调用api给卡片充值
    // ChargeonlineApi: function () {
    //   let that = this
    //   console.log("你在进行api请求")
    //   console.log("userid:", app.globalData.userId)
    //   wx.request({
    //     url: 'http://localhost:8001/card/unbindUser',
    //     // url: ApiRootUrl + 'card/unbindUser',
    //     data: {
    //       userId: app.globalData.userId,
    //       cardUid: that.data.cardUid,
    //     },
    //     method: 'PUT',
    //     header: {
    //       'content-type': 'application/json',
    //       'chartset': 'utf-8'
    //     },
    //     success(res) {
    //       console.log("获取数据成功", res)
    //       console.log("code:", res.data.code)
    //       that.setData({
    //         msg: res.data.message
    //       })
    //       console.log("res.data.message", res.data.message)
    //       if (res.data.code == 200) {
    //         that.openToast()
    //       }
    //       if (res.data.code == 400) {
    //         that.openWarnToast()
    //       }
    //       if (res.data.code == 401) {
    //         that.open401Toast()
    //       }
    //     }
    //   })
    // },
    // closeChargeonline: function () {
    //   this.setData({
    //     androidDialog2_unbinduser: false,
    //   })
  
    // },
    // confirmChargeonline: function () {
    //   console.log("你点击了确定充值按钮")
    //   this.setData({
    //     androidDialog2_unbinduser: false,
    //   })
    //   this.ChargeonlineApi()
    // },

  //成功弹窗
  openToast: function () {
    this.setData({
      toast: true
    });
    setTimeout(() => {
      this.setData({
        hideToast: true
      });
      setTimeout(() => {
        this.setData({
          toast: false,
          hideToast: false,
        });
      }, 300);
    }, 3000);
  },
  //操作失败弹框
  openWarnToast: function () {
    this.setData({
      warnToast: true
    });
    setTimeout(() => {
      this.setData({
        hidewarnToast: true
      });
      setTimeout(() => {
        this.setData({
          warnToast: false,
          hidewarnToast: false,
        });
      }, 300);
    }, 3000);
  },
})